package com.ls.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ls.client.UserFeignClient;
import com.ls.mapper.OrderMapper;
import com.ls.pojo.Order;

import com.ls.pojo.Product;
import com.ls.pojo.User;
import com.ls.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xiaotang
 * @since 2023-04-07
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderService {

    @Resource
    OrderMapper orderMapper;

    @Autowired
    UserFeignClient userFeignClient;





    @Override
    public Order saveOrder(Product product, Integer number, Integer userId) {
        BigDecimal price = product.getPrice();
        BigDecimal bignumber=new BigDecimal(number.toString());
        BigDecimal multiply = price.multiply(bignumber);
        System.out.println("multiply = " + multiply);
       User user = userFeignClient.findById(userId).getData();

        Order order = new Order();
        order.setUserId(userId);
        order.setProductId(product.getId());
        order.setNumber(number);
        order.setPrice(price);
        order.setAmount(multiply);
        order.setProductName(product.getProductName());
        order.setUsername(user.getUsername());
        orderMapper.insert(order);
        return order;
    }
}
